package com.zhgxi.openfire;

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA.
 * User: xi
 * Date: 13-12-3
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
public class ReciveAllMessage {


    public static void main(String[] args) throws XMPPException, IOException {
        ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1", 5222);
        final Connection connection = new XMPPConnection(config);
        connection.connect();
        String userName = "android7";
        String password = "111111";
        connection.login(userName, password);

        Presence presence = new Presence(Presence.Type.available);   //这里如果改成unavailable则会显示用户不在线
        presence.setStatus("Go fishing");
        connection.sendPacket(presence);
        connection.getRoster();

        ChatManager chatManager = connection.getChatManager();

        final MessageListener messagelistener = new MessageListener() {
            @Override
            public void processMessage(Chat chat,
                                       Message message) {
                System.out.println(connection.getUser() + " Received message:" + message.getBody());
                try {
                    chat.sendMessage("hhhh");
                } catch (XMPPException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

//                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//                System.out.println(connection.getUser() + " Please input the message:");
//                String msg = "";
//                try {
//                    while (!(msg = br.readLine()).equals("exit")) {
//
//                        chat.sendMessage(msg);
//                        System.out.println(connection.getUser() + " Please input the message:");
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                } catch (XMPPException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                }
            }

        };
        chatManager.addChatListener(
                new ChatManagerListener() {
                    @Override
                    public void chatCreated(Chat chat,
                                            boolean createdLocally) {
                        if (!createdLocally)
                            chat.addMessageListener(messagelistener);
                    }
                });


        while (true) {

        }
       // connection.disconnect();
    }
}
